<template>
  <div style="height: 100%;background-color: #132E3F;">
  <div class="nowWeather" style="background-color: #132E3F;">
    <div class="tit"style="background-color: #132E3F;">
      <span style="color: white;">{{ dateStr }}</span>
      <span style="color: white;">{{ position }}</span>
      <span style="color: white;">{{ '今日天气' }}</span>
    </div>
    <div class="con"style="background-color: #132E3F;">
      <div class="left"style="background-color: #132E3F;">
        <div class="top"style="background-color: #132E3F;">
          <p style="color: white;">{{ weatherData.realtime.temperature }}<span>℃</span></p>
          <p style="color: white;">{{ weatherData.future[0].temperature }}{{ weatherData.future[0].weather }}</p>
        </div>
        <div class="bom"style="background-color: #132E3F;">
          <img :src="src" alt="" />
        </div>
      </div>

      <ul class="right"style="background-color: #132E3F;">
        <li v-for="(value, key) in weatherData.realtime" :key="key" style="color: white;">
          <span class="dec" style="color: white;">{{ key }}</span
          ><br  />{{ value }}
        </li>
      </ul>
    </div>
    <div class="foot"style="background-color: #132E3F;">
      <div>
        <span style="color: white;">{{ '数据来源: 天气预报' }}</span
        ><span style="color: white;">{{ '更新时间: ' }}{{ date.toLocaleString() }}</span>
      </div>
    </div>
  </div>
  <div class="future"style="background-color: #132E3F;">
    <h4 style="color: white;">{{ '未来五天天气预报' }}</h4>
    <div class="futureWeather" style="color: white;">
      <div class="key" style="color: white;">
        <div v-for="(key, index) in Object.keys(weatherData.future[0])" :key="index">
          {{ key }}
        </div>
      </div>
      <div v-for="(day, index) in weatherData.future" :key="index" class="value">
        <div v-for="(value, key) in weatherData.future[index]" :key="key">{{ value }}</div>
      </div>
    </div>
  </div>
  </div>
</template>

<script lang="ts" setup>
interface IWeather {
  city: string
  realtime: {
    aqi: string
    direct: string
    humidity: string
    info: string
    power: string
    temperature: string
    wid?: string
  }
  future: {
    date: string
    temperature: string
    weather: string
    direct: string
    wid?: { [key: string]: string }
  }[]
}
import axios from 'axios'
import { ref, onMounted, type Ref } from 'vue'
const weatherData = ref<IWeather>({
  city: '',
  realtime: {
    aqi: '26',
    direct: '东北风',
    humidity: '97',
    info: '大雨',
    power: '3级',
    temperature: '21',
  },
  future: [
    {
      date: '2025-08-27',
      temperature: '20/23℃',
      weather: '暴雨转中雨',

      direct: '东风转北风',
    },
    {
      date: '2025-08-28',
      temperature: '22/27℃',
      weather: '阴',
      direct: '北风转南风',
    },
    {
      date: '2025-08-29',
      temperature: '24/28℃',
      weather: '阴转多云',
      direct: '北风转南风',
    },
    {
      date: '2025-08-30',
      temperature: '24/29℃',
      weather: '多云',
      direct: '北风转南风',
    },
    {
      date: '2025-08-31',
      temperature: '21/29℃',
      weather: '阵雨转阴',
      direct: '北风转南风',
    },
  ],
})
const aa: Ref<{ wid: string; weather: string; src?: string }[]> = ref([])
const src = ref<string | undefined>()
async function getWeather() {
  const response = await axios.get('/weather/simpleWeather/query', {
    params: {
      city: '北京',
      key: 'b0b391a18b36ce75e9d7e5ad4ecc76f3',
    },
  })
  return response.data.result
}

async function getWeatherMock() {
  const response = await axios.get('/api/weather')
  return response.data
}

onMounted(async () => {
  weatherData.value = await getWeather()
  //   console.log(weatherData.value)
  delete weatherData.value.realtime['wid']
  weatherData.value.future.forEach((item) => {
    delete item['wid']
  })
  aa.value = await getWeatherMock()
  src.value = aa.value.filter(
    (item: any) => item.weather == weatherData.value.realtime.info,
  )[0]?.src
})

const date = new Date()
const formatDate = (d: Date) => {
  const y = d.getFullYear()
  const m = String(d.getMonth() + 1).padStart(2, '0')
  const day = String(d.getDate()).padStart(2, '0')
  const week = ['日', '一', '二', '三', '四', '五', '六'][d.getDay()]
  return `${y}年${m}月${day}日 星期${week}`
}
const dateStr = formatDate(date)
const position = '北京'
</script>

<style lang="scss" scoped>
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  list-style: none;
  text-decoration: none;
}
.nowWeather {
  background-color: #f0f0f0;
  padding: 16px;
  border-radius: 8px;
  margin-bottom: 16px;
  width: 452px;

  .tit {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 8px;
    display: flex;
    justify-content: space-between;
    span {
      margin-right: 10px;
    }
  }

  .con {
    font-size: 14px;
    display: flex;
    .left {
      width: 120px;
      background-color: #999;
      flex: none;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      text-align: center;
      .top {
        height: 80px;
      }
      p:first-child {
        margin-top: -5px;
        font-size: 25px;
        span {
          font-size: 20px;
        }
      }
      p:last-child {
        font-size: 12px;
        color: #eee;
      }
    }
    .right {
      width: 300px;
      background-color: #f0f0f0;
      display: flex;
      flex-wrap: wrap;
      li {
        width: 100px;
        height: 80px;
        line-height: 40px;
        text-align: center;
        font-size: 16px;
        .dec {
          color: #999;
        }
      }
    }
  }

  .foot {
    font-size: 12px;
    color: #999;
    margin-top: 10px;
    span {
      display: inline-block;
      margin-right: 15px;
    }
  }
}

.future {
  width: 800px;
  padding: 16px;
  border-radius: 8px;
  background-color: #f9f9f9;
  .futureWeather {
    display: flex;
    height: 300px;
    & > div {
      display: flex;
      flex-direction: column;
      align-items: center;
    }
    .key {
      width: 100px;
      height: 60px;
      line-height: 60px;
    }
    .value {
      width: 150px;
      height: 60px;
      line-height: 60px;
    }
  }
}
</style>
